package ru.acme.lab14;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;

import ru.acme.lab14.domain.account.CheckingAccount;
import ru.acme.lab14.domain.account.SavingAccount;
import ru.acme.lab14.domain.bank.Bank;
import ru.acme.lab14.domain.client.Client;
import ru.acme.lab14.domain.client.Gender;
import ru.acme.lab14.net.BankServer;
import ru.acme.lab14.service.bank.BankService;
import ru.acme.lab14.service.command.CommandHandler;

public class BankApplication {
	
	private static final String BANK_SERIALIZATION_FILE_NAME = "C:\\bank.dat";
	private static final int SERVER_PORT = 4444;
	
	public static void main(String[] args) {

		String feedFileName = null;
		boolean isServer = false;
		for (int i = 0; i < args.length; ++i) {
			if ((args[i].equals("-loadfeed")) && (i < args.length - 1)) {
				feedFileName = args[i + 1];
			}
			if (args[i].equals("-server")) {
				isServer = true;
			}
		}
		Bank bank = new Bank();
		if (feedFileName != null) {
			try {
				BankService.loadClientsFromFile(bank, feedFileName);
			} catch (FileNotFoundException e) {
				System.out.println("File " + feedFileName + " not found");
			} catch (ParseException e) {
				System.out.println("File " + feedFileName + " contains incorrect string: " + e.getMessage());
			}
		}		
		
		if (isServer) {
			new Thread(new BankServer(bank, SERVER_PORT)).start();
		}
		new Thread(new CommandHandler(bank, true, System.in, System.out)).start();
	}

}
